<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;

/**
 * Word Theme for Typecho
 * 一个模拟 Microsoft Word 文档风格的 Typecho 主题，提供简洁、专业的阅读体验。
 * 
 * @package Word Theme
 * @author cklx0719
 * @version 1.0.0
 * @link https://github.com/cklx0719
 */

/**
 * 主题设置
 */
function themeConfig($form) {
    // 基础设置
    $icp_record = new Typecho_Widget_Helper_Form_Element_Text('icp_record', NULL, '', _t('备案号'), _t('如果有备案号，请填写，将显示在网站底部并链接到工信部网站'));
    $form->addInput($icp_record);
    
    // 文章设置
    $content_theme = new Typecho_Widget_Helper_Form_Element_Select(
        'content_theme',
        array(
            'prose-gray' => _t('灰色'),
            'prose-slate' => _t('板岩色'),
            'prose-zinc' => _t('锌色'),
            'prose-neutral' => _t('中性色'),
            'prose-stone' => _t('石色')
        ),
        'prose-gray',
        _t('颜色主题'),
        _t('选择文章内容的颜色主题')
    );
    $form->addInput($content_theme);
    
    // 布局设置
    $show_header_footer = new Typecho_Widget_Helper_Form_Element_Checkbox(
        'show_header_footer',
        array('true' => _t('显示页眉页脚')),
        array('true'),
        _t('显示页眉页脚'),
        _t('在文章详情页显示页眉页脚')
    );
    $form->addInput($show_header_footer);
    
    // 样式设置
    $heading_style = new Typecho_Widget_Helper_Form_Element_Select(
        'heading_style',
        array(
            'default' => _t('默认(一级黑体,二级楷体,三四级仿宋)'),
            'all-bold' => _t('全部黑体'),
            'all-same' => _t('全部相同')
        ),
        'default',
        _t('标题样式'),
        _t('选择文章标题的样式')
    );
    $form->addInput($heading_style);
}

/**
 * 输出评论回复的链接
 */
function themeReply($comments) {
    $commentClass = $comments->authorId == $comments->ownerId ? 'comment-by-author' : '';
    $commentLevelClass = $comments->levels > 0 ? ' comment-child' : ' comment-parent';
    
    echo '<div id="' . $comments->theId . '" class="comment-body' . $commentLevelClass . ' ' . $commentClass . '">';
    echo '<div class="comment-author">';
    echo '<span class="comment-author-name">' . $comments->author . '</span>';
    echo '<span class="comment-date">' . date('Y年m月d日', $comments->created) . '</span>';
    echo '</div>';
    echo '<div class="comment-content">' . $comments->content . '</div>';
    echo '<div class="comment-reply">';
    echo '<a href="' . $comments->permalink . '#respond" rel="nofollow" onclick="return TypechoComment.reply(\''. $comments->theId .'\')">回复</a>';
    echo '</div>';
    echo '</div>';
}

/**
 * 获取文章阅读次数
 */
function getPostView($archive) {
    $cid = $archive->cid;
    $db = Typecho_Db::get();
    $prefix = $db->getPrefix();
    
    if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) {
        $db->query('ALTER TABLE `' . $prefix . 'contents` ADD `views` INT(10) DEFAULT 0;');
    }
    
    $row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid));
    if ($archive->is('single')) {
        $views = Typecho_Cookie::get('extend_contents_views');
        if (empty($views)) {
            $views = array();
        } else {
            $views = explode(',', $views);
        }
        
        if (!in_array($cid, $views)) {
            $db->query($db->update('table.contents')->rows(array('views' => (int)$row['views'] + 1))->where('cid = ?', $cid));
            array_push($views, $cid);
            $views = implode(',', $views);
            Typecho_Cookie::set('extend_contents_views', $views);
        }
    }
    
    return $row['views'];
}